package com.example.service.impl;

import com.example.common.result.Result;
import com.example.entity.mes.Equipment;
import com.example.entity.mes.Unit;
import com.example.mapper.EquipmentMapper;
import com.example.mapper.UnitMapper;
import com.example.service.EquipmentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 14501
* @description 针对表【equipment】的数据库操作Service实现
* @createDate 2024-03-06 11:38:20
*/
@Service
@Slf4j
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService{

    @Resource
    private EquipmentMapper equipmentMapper;
    @Resource
    private UnitMapper unitMapper;

    /**
     * 分页条件查询
     * @param current
     * @param size
     * @param code
     * @param name
     * @return
     */
    @Override
    public Result queryIPageList(Integer current, Integer size, String code, String name, String uid) {
        try {
            Map<String, Object> map = new HashMap<>();
            QueryWrapper<Equipment> equipmentQueryWrapper = new QueryWrapper<>();
            if (name != null && !name.isEmpty()) {
                equipmentQueryWrapper.like("name", name);
            }
            if (code != null && !code.isEmpty()) {
                equipmentQueryWrapper.eq("code", code);
            }
            if (uid != null && !uid.isEmpty()) {
                Unit unit = this.unitMapper.selectById(uid);
                List<Equipment> equipmentList = new ArrayList<>();
                if (unit != null) {
                    equipmentQueryWrapper.eq("parentid", unit.getId());
                    List<Unit> unitList = this.unitMapper.selectList(new QueryWrapper<Unit>().eq("parentid", unit.getId()));
                    equipmentList = unitList.stream()
                            .flatMap(u -> this.equipmentMapper.selectList(equipmentQueryWrapper.eq("uid", u.getId())).stream())
                            .collect(Collectors.toList());
                    if (equipmentList.isEmpty()) {
                        return Result.error("暂无数据");
                    }
                    map.put("pages", unitList.size());
                    map.put("total", (long) equipmentList.size());
                    map.put("data", equipmentList);
                    return Result.success(map);
                }
            }
            IPage<Equipment> config = new Page<>(current, size);
            IPage<Equipment> page = this.equipmentMapper.selectPage(config, equipmentQueryWrapper);
            List<Equipment> equipmentList = page.getRecords();
            long total = page.getTotal();
            map.put("pages", page.getPages());
            map.put("total", total);
            map.put("data", equipmentList);
            if (total == 0) {
                return Result.error("未查询到数据", "500");
            }
            return Result.success(map);
        } catch (Exception e) {
            log.error("【分页查询发生异常】 { 错误信息:"+"{"+e+"}"+" }");
            return Result.error("分页查询发生异常: " + e.getMessage(), "500");
        }
    }
}




